package oceanlife.log;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import oceanlife.evostrat.GeneCrossing;
import oceanlife.pathfinding.Pillar;

public class ImageDrawer {
	private Graphics2D g;
	private BufferedImage bi;
	private int width;
	private int height;
	private String name;
	private String desc;
	private int wallThickness = 20;
	private int textSpacing = 20;
	private int ySpacing = 20;

	
	public ImageDrawer(ArrayList<Double> bestSolution, String kind, String name, String desc) {
		this.desc = desc;
		this.name = name;
		if (kind.equals("Straight")) {
			width = (int) (bestSolution.size()/2 * Pillar.spacing + Pillar.spacing);
			height = Pillar.maxPosition.intValue() + wallThickness * 2 + textSpacing;
			bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

			g = bi.createGraphics();
			drawStraight(bestSolution);
		}
		if (kind.equals("Crossing")) {
			width = (int)GeneCrossing.xSize;
			height = (int)GeneCrossing.ySize+ySpacing;
			bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

			g = bi.createGraphics();
			drawCross(bestSolution);
		}
	}

	public void drawStraight(ArrayList<Double> bestSolution) {
		int teller = 0;
		ArrayList<Shape> shapes = new ArrayList<Shape>();
		shapes.add(new Rectangle(0,textSpacing,width-1,wallThickness-1));
		shapes.add(new Rectangle(0,textSpacing+(int)(wallThickness+Pillar.maxPosition),width-1,wallThickness-1));
		System.out.println(name);
		while (teller * 2 < bestSolution.size()) {
			System.out.println(bestSolution.get(teller * 2)+" "+bestSolution.get(teller * 2+1));
			Shape s = new Rectangle(
					(int) (70 + teller * Pillar.spacing - bestSolution.get(teller * 2 + 1)),
					(int) (wallThickness + bestSolution.get(teller * 2) - bestSolution.get(teller * 2 + 1)) + textSpacing, 
					(int) (bestSolution.get(teller * 2 + 1) * 2), 
					(int) (bestSolution.get(teller * 2 + 1) * 2));
			shapes.add(s);
			teller++;
		}
		
		Font font = new Font("LMRoman12-Regular", Font.BOLD, 20); 
		g.setFont(font);
		FontMetrics fontMetrics = g.getFontMetrics(); 
		int stringWidth = fontMetrics.stringWidth(desc);
		int stringHeight = fontMetrics.getAscent();
		g.setPaint(Color.black);
		g.drawString(desc, 0, stringHeight/2+5);
		writeImage(shapes);
	}

	public void drawCross(ArrayList<Double> bestSolution) {
		ArrayList<Shape> shapes = new ArrayList<Shape>();
		int corlength = (int)GeneCrossing.corLength;
		shapes.add(new Rectangle(0,ySpacing,corlength,corlength));
		shapes.add(new Rectangle(width-corlength,ySpacing,corlength,corlength));
		shapes.add(new Rectangle(0,width-corlength+ySpacing,corlength,corlength));
		shapes.add(new Rectangle(width-corlength,width-corlength+ySpacing,corlength,corlength));

		for(int i = 0; i < bestSolution.size(); i+=2){
			shapes.add(new Rectangle(corlength+(int)(bestSolution.get(i)-0),corlength+(int)(bestSolution.get(i+1)-0)+ySpacing,(int)(GeneCrossing.radius*2),(int)(GeneCrossing.radius*2)));
		}
		Font font = new Font("LMRoman12-Regular", Font.BOLD, 20); 
		g.setFont(font);
		FontMetrics fontMetrics = g.getFontMetrics(); 
		int stringWidth = fontMetrics.stringWidth(desc);
		int stringHeight = fontMetrics.getAscent();
		g.setPaint(Color.black);
		g.drawString(desc, 0, stringHeight/2+5);
		writeImage(shapes);
	}

	public void writeImage(ArrayList<Shape> shapes) {
		try {
			g.setPaint(Color.gray);

			for (Shape s : shapes) {
				g.draw(s);
				g.fill(s);
			}

			ImageIO.write(bi, "PNG", new File(
					"C:/Users/Marijn/workspaceJAVAnew/OCEAN LIFE/linux/logs/"
							+ name + ".PNG"));

		} catch (IOException ie) {
			ie.printStackTrace();
		}
	}

	public static void main(String[] args) {
		for(Font f : java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()){
			System.out.println(f.getName());
		}
	}
}